﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace WebKpiPage.Data.TotalSearch
{
    public  class SplitDatas
    {
        /// <summary>
        /// KEY为类型名称（类型分为：预定义指标、自定义指标、Counter、参数） VALUE为字典（key为DEV_CAT,value为指标集ID集合）
        /// </summary>
        private static Dictionary<string, Dictionary<string, List<string>>> 
            newNodeNameDevCatSetIDMap = new Dictionary<string, Dictionary<string, List<string>>>();

        private static Dictionary<string, TimeDescriptionHelper.AttributeSet> newAttributeSetIDMap
            = new Dictionary<string, TimeDescriptionHelper.AttributeSet>();
        /// <summary>
        /// 用于预定义指标查找 KEY为DEV_CAT VALU为字典（key为指标集名称简写,value为指标集的ID集合）
        /// </summary>
        private static Dictionary<string, Dictionary<string, List<string>>> 
            newDEVCATSubNameIDListMap = new Dictionary<string, Dictionary<string, List<string>>>();

        private static Dictionary<string, TimeDescriptionHelper.UserAttributeSet> newUserAttributeSetIDMap
            = new Dictionary<string, TimeDescriptionHelper.UserAttributeSet>();
        /// <summary>
        /// 用于Counter指标查找 KEY为DEV_CAT VALU为字典（key为指标集名称简写,value为指标集的ID集合）
        /// </summary>
        private static Dictionary<string, Dictionary<string, List<string>>> 
            newDEVCATCounterNameIDListMap = new Dictionary<string, Dictionary<string, List<string>>>();

        private static Dictionary<string, TimeDescriptionHelper.CounterOBJTPYE> newCounterOBJTPYEIDMap =
            new Dictionary<string, TimeDescriptionHelper.CounterOBJTPYE>();

        private static Dictionary<string, List<TimeDescriptionHelper.ATTRIBUTE>> newAttributeListSetIDMap =
            new Dictionary<string, List<TimeDescriptionHelper.ATTRIBUTE>>();

        private static Dictionary<string, List<TimeDescriptionHelper.UserAttribute>> newUserAttributeListSetIDMap =
            new Dictionary<string, List<TimeDescriptionHelper.UserAttribute>>();

        private static Dictionary<string, List<TimeDescriptionHelper.Counter>> newCounterListSetIDMap =
            new Dictionary<string, List<TimeDescriptionHelper.Counter>>();

        private static Dictionary<string, List<TimeDescriptionHelper.Counter>> newWithoutBSCCounterListSetIDMap =
            new Dictionary<string, List<TimeDescriptionHelper.Counter>>();

        private static Dictionary<int, List<string>> netTypeVisibleMap = new Dictionary<int, List<string>>();

        private static void ClearNewMap()
        {
            newNodeNameDevCatSetIDMap.Clear();
            newAttributeSetIDMap.Clear();
            newDEVCATSubNameIDListMap.Clear();
            newUserAttributeSetIDMap.Clear();
            newCounterOBJTPYEIDMap.Clear();
            newAttributeListSetIDMap.Clear();
            newUserAttributeListSetIDMap.Clear();
            newCounterListSetIDMap.Clear();
            newWithoutBSCCounterListSetIDMap.Clear();
            newNodeNameDevCatSetIDMap["预定义指标"] = new Dictionary<string, List<string>>();
            newNodeNameDevCatSetIDMap["自定义指标"] = new Dictionary<string, List<string>>();
            newNodeNameDevCatSetIDMap["Counter"] = new Dictionary<string, List<string>>();
            newNodeNameDevCatSetIDMap["参数"] = new Dictionary<string, List<string>>();
        }

        /// <summary>
        /// 解析所有返回的数据的qry_result列，将“,”和“@”间隔符分开的字符串拆分
        /// </summary>
        /// <returns></returns>
        public static ArrayList SplitWSearchPrm()
        {
            ArrayList RstListArray = new ArrayList();
            DataTable dt = TotalSearchDB.GetInfoForInitPage();
            ClearNewMap();
            for (int i = 0; i < dt.Rows.Count; i++)
			{
                string obj = dt.Rows[i]["qry_result"].ToString();
                int row = Convert.ToInt32(dt.Rows[i]["qry_id"].ToString());
                switch (row)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 5:
                    case 7:
                    case 10:
                    case 11:
                        RstListArray.Add(ListAddRange(obj, row));
                        break;
                    case 33:
                        GetAllSearchPrm(obj);
                        RstListArray.Add(newNodeNameDevCatSetIDMap);
                        break;
                    case 44:
                        InitAllColumnList(obj);
                        RstListArray.Add(newAttributeListSetIDMap);
                        break;
                }
			}
            return RstListArray;
        }

        /// <summary>
        /// 获得结果列表
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="row"></param>
        private static List<string> ListAddRange(string obj,int row)
        {
            if (!netTypeVisibleMap.ContainsKey(row))
            {
                netTypeVisibleMap[row] = new List<string>();
            }
            if (obj != null)
            {
                string[] array = obj.ToString().Split(',');
                netTypeVisibleMap[row].Clear();
                netTypeVisibleMap[row].AddRange(array);
            }
            return netTypeVisibleMap[row];
        }

        /// <summary>
        /// 获取数据表字段列表
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static void InitAllColumnList(string obj)
        {
            if (obj != null)
            {
                string[] array = obj.ToString().Split(',');
                foreach (string str in array)
                {
                    string[] value = str.Split('@');
                    if (value.Length == 6)
                    {
                        TimeDescriptionHelper.ATTRIBUTE attribute = 
                            new TimeDescriptionHelper.ATTRIBUTE(value[0], value[2], value[5]);
                        attribute.SetID = value[1];
                        attribute.TABLE_COL = value[3];
                        attribute.DISPLAY_TYPE = value[4];

                        if (newAttributeListSetIDMap.ContainsKey(attribute.SetID))
                        {
                            newAttributeListSetIDMap[attribute.SetID].Add(attribute);
                        }
                        else
                        {
                            newAttributeListSetIDMap[attribute.SetID] = 
                                new List<TimeDescriptionHelper.ATTRIBUTE>();
                            newAttributeListSetIDMap[attribute.SetID].Add(attribute);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 解析所有查询参数
        /// </summary>
        /// <param name="obj"></param>
        private static void GetAllSearchPrm(string obj)
        {
            if (obj != null)
            {
                string[] array = obj.ToString().Split(',');
                foreach (string str in array)
                {
                    string[] value = str.Split('@');
                    if (value.Length == 7)
                    {
                        TimeDescriptionHelper.AttributeSet attributeSet 
                            = new TimeDescriptionHelper.AttributeSet(value[0], value[1]);
                        attributeSet.TIME_SEGMENT = value[2];
                        attributeSet.NE_CAT = value[3];
                        attributeSet.CAPA_CAT = value[4];
                        attributeSet.REF_CAT = value[5];
                        attributeSet.DEV_CAT = value[6];

                        string name = null;
                        string[] nameSplit = attributeSet.Name.Split('_');
                        for (int index = 0; index < nameSplit.Length; index++)
                        {
                            if (index == 0)
                            {
                                name = nameSplit[index];
                            }
                            else if (index != 1 && index != 2)
                            {
                                name += "_" + nameSplit[index];
                            }
                        }

                        if (newDEVCATSubNameIDListMap.ContainsKey(attributeSet.DEV_CAT))
                        {
                            if (newDEVCATSubNameIDListMap[attributeSet.DEV_CAT].ContainsKey(name))
                            {
                                newDEVCATSubNameIDListMap[attributeSet.DEV_CAT][name].Add(attributeSet.ID);
                            }
                            else
                            {
                                newDEVCATSubNameIDListMap[attributeSet.DEV_CAT][name] = new List<string>();
                                newDEVCATSubNameIDListMap[attributeSet.DEV_CAT][name].Add(attributeSet.ID);
                            }
                        }
                        else
                        {
                            newDEVCATSubNameIDListMap[attributeSet.DEV_CAT] = new Dictionary<string, List<string>>();
                            newDEVCATSubNameIDListMap[attributeSet.DEV_CAT][name] = new List<string>();
                            newDEVCATSubNameIDListMap[attributeSet.DEV_CAT][name].Add(attributeSet.ID);
                        }

                        if (newNodeNameDevCatSetIDMap["预定义指标"].ContainsKey(attributeSet.DEV_CAT))
                        {
                            if (!newNodeNameDevCatSetIDMap["预定义指标"][attributeSet.DEV_CAT].Contains(attributeSet.ID))
                            {
                                newNodeNameDevCatSetIDMap["预定义指标"][attributeSet.DEV_CAT].Add(attributeSet.ID);
                            }
                        }
                        else
                        {
                            newNodeNameDevCatSetIDMap["预定义指标"][attributeSet.DEV_CAT] = new List<string>();
                            newNodeNameDevCatSetIDMap["预定义指标"][attributeSet.DEV_CAT].Add(attributeSet.ID);
                        }
                        newAttributeSetIDMap[attributeSet.ID] = attributeSet;
                    }
                }
            }
        }
    }
}
